let Controller = {
  mounted() {
    this.initData();
  },
  activated() {
    this.initData();
    this.requestDetail();
    this.onDownloadExcel();
  },
  methods: {
    initData() { 
      const param = JSON.parse(this.$route.query);
      this.Model.queryData = _.cloneDeep(param.row);
      this.Model.detailFlag = Boolean(param.flag);
    },
    onUploadFileChange(e) {
      if (e.target.files.length == 0) {
        return;
      }
      this.Model.uploadFile = e.target.files[0];
    },
    onDownloadExcel() {
      this.$ajax.get(this.apiTest + `/supplier/whWarehouseDetail/getGoodsExcelInfo`)
        .then(res => {
          const data = res.data;
          if (data.status == 200) {
            this.Model.fileUrl = data.result.fileUrl;
            this.Model.fileName = data.result.fileName;
          }
        })
    },
    getFilterData(val) {
      this.Model.searchFromData = val;
      console.log(this.Model.searchFromData)
    },
    requestDetail() {
      const number = this.Model.queryData.number;
      const param = {
        condition: {
          number
        }
      }
      if (number) this.$ajax
        .post(this.apiTest + `/supplier/whWarehouseDetail/outAndInDetails`, param)
        .then(response => {
          const data = response.data;
          if (data.status == 200) {
            const result = data.result;
            this.Model.status = result.status;
            this.Model.whWarehouseId = result.whWarehouseId;
            this.Model.tabFormInline = [{
                label: '采购入库单号',
                value: result.number
              },
              {
                label: '单据状态',
                value: result.status
              },
            ];
            this.Model.detailData.shouldSaleGoodsInfoVos = result.shouldSaleGoodsInfoVos;
            this.Model.detailData.actualSaleGoodsInfoVos = result.actualSaleGoodsInfoVos;
            const dataArr = [
              result.type, 
              result.originNumber, 
              result.supplierName, 
              result.inWarehouseName,
              result.createName,
              result.operatorName,
              result.remark
            ];
            for(let i =0;i<dataArr.length;i++){
              this.Model.filterOption.items[i].data = dataArr[i];
            }
           /*  this.Model.filterOption = {
              items: [{
                  label: "入库单类型:",
                  type: "text",
                  data: result.type,
                  placeholder: "请选择",
                  value: undefined,
                  key: "type"
                },
                {
                  label: "到货通知单:",
                  type: "text",
                  data: result.originNumber,
                  placeholder: "请选择",
                  value: undefined,
                  key: "originNumber"
                },
                {
                  label: "供应商:",
                  type: "text",
                  data: result.supplierName,
                  placeholder: "顾客姓名/顾客ID/手机号码",
                  value: undefined,
                  key: "supplierName"
                },
                {
                  label: "入库仓库:",
                  type: 'text',
                  data: result.inWarehouseName,
                  placeholder: "请选择",
                  value: undefined,
                  key: "locationName",
                  required: true
                },
                {
                  label: "创建人:",
                  type: "text",
                  data: result.createName,
                  placeholder: "请选择",
                  value: undefined,
                  key: "createName"
                },
                {
                  label: "操作人:",
                  type: "text",
                  data: result.operatorName,
                  placeholder: "入库单号/专属编码/条码/售后单号",
                  value: undefined,
                  key: "operatorName"
                },
                {
                  label: "备注:",
                  type: "text",
                  data: result.remark,
                  placeholder: "",
                  value: undefined,
                  key: "remark"
                },
              ],
            }; */
            // this.setStepData(result.saleOrderStatusList, result.aftersaleStatus);
          } else {
            this.$message.error(data.msg);
          }

        })
        .catch(error => {
          this.$message.error('请求错误');
        });
    },
    toBarcodeDetail(row) {
      //跳转条码详情
      const name = 'purchaseWarehousingBarcodeDetail';
      this.$router.push({
        name: name,
        query: {
          data: row
        }
      })
    },
    setStepData(list, status) {
      let activeStep = 0,
        step = [];
      for (let i = 0; i < list.length; i++) {
        if (list[i].code == status) activeStep = i;
        step.push({
          title: list[i].codeName,
          description: list[i].createTime
        })
      };
      this.Model.stepOption = {
        activeStep,
        step
      }
    },

    onDeleteExportData() {
      const eList = this.Model.exportTableSelectList;
      if (eList.length == 0) return;
      const eLen = eList.length;
      let aList = _.cloneDeep(this.Model.detailData.actualSaleGoodsInfoVos);

      const aLen = aList.length;
      for (let i = 0; i < eLen; i++) {
        for (let j = 0; j < aLen; j++) {
          if (aList[j] !== undefined) {
            if (eList[i].id == aList[j].id) {
              aList[j] = undefined;
            }
          }

        }
      };
      aList = aList.filter(a => a);
      this.$set(this.Model.detailData, 'actualSaleGoodsInfoVos', aList);
    },
    onExportMaterial() {
      this.Model.dialogFormVisible = true;
    },
    onSureWarehousing() {
      const list = this.Model.exportTableSelectList || [];
      if (list.length == 0) return;
      const ids = list.map(item => {
        return item.id
      });
      if (!this.Model.searchFromData) {
        this.$message.warning('请选择入库仓库！');
        return;
      }
      const param = {
        ids,
        whWarehouseId: this.Model.whWarehouseId,
        locationId: this.Model.searchFromData.locationName,
      }
      this.$ajax.post(this.apiTest + `/supplier/whWarehouseDetail/inWarehouse`, param)
        .then(res => {
          const data = res.data;
          if (data.status == 200) {
            this.$message.success('入库成功！');
            this.$router.push({
              name: `purchaseWarehousing`,
            })
          } else {
            this.$message.error(data.msg)
          }
        }).catch(err => {
          this.$message.error('请求失败');
        })
    },
    onTableSelectionChange(val, index, a) {
      this.Model.exportTableSelectList = val;
    },
    onUploadSureFile() {
      /*  */
      const file = this.Model.uploadFile;
      const number = this.Model.queryData.number;
      let fileName;
      if (file && !file.name) {
        this.$message({
          type: 'error',
          message: '还没有选择要上传的文件!'
        });
        return;
      } else {
        const extension = file.name.split('.')[1] === 'xls';
        const extension2 = file.name.split('.')[1] === 'xlsx';
        const isLt2M = file.size / 1024 / 1024 < 5
        if (!extension && !extension2) {
          this.$message.warning('上传模板只能是 xls、xlsx格式!')
          return;
        }
        fileName = file.name;
      }
      let fileFormData = new FormData();
      fileFormData.append('file', file, fileName); //filename是键，file是值，就是要传的文件，test.zip是要传的文件名
      fileFormData.append('number', number); //filename是键，file是值，就是要传的文件，test.zip是要传的文件名
      let requestConfig = {
        headers: {
          'Content-Type': 'multipart/form-data'
        },
      }
      this.$ajax.post(this.apiTest + `/supplier/whWarehouseDetail/importPurchaseWarehouseExcel`, fileFormData, requestConfig)
        // this.$ajax.post(this.apiTest + `/supplier/whWarehouseDetail/importPurchaseWarehouseExcel`, fileFormData, requestConfig)
        .then(res => {
          const data = res.data;
          if (data.status == 200) {
            this.$message({
              message: '上传成功',
              type: 'success',
            });
            this.Model.detailData.actualSaleGoodsInfoVos = data.result;
            this.Model.dialogFormVisible = false;
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    cardClick(color) {
      this.Model.cardSelected = color;
    },
    showChild(val) {
      this.Model.searchFromData = val;
    },
    toSearch(val) {
      console.log(val)
    },
    toClear(val) {
      this.Model.searchFromData = this.Model.defaultSearchFromData;
    },
    toExport(val) {
      console.log(val);
    },
    onTabChange(val) {
      switch (val) {
        case 1:
          break;
        case 2:
          break;
        case 3:
          break;
      }
    },
    requestSaleGoodsInfoByEntity() {
      const param = {
        batchNo: this.Model.exportBarcode
      };
      this.$ajax.post(this.apiTest + `/supplier/whWarehouseDetail/getByKeyWord`, param)
        .then(res => {
          const data = res.data;

          if (data.status == 200) {
            const result = data.result;
            this.Model.detailData.actualSaleGoodsInfoVos = [result];
          }
        }).catch(err => {
          console.log(err)
        })
    },
    toggleTableDetail(val) {
      const obj = this.Model[val];
      if (obj.flag === true) {
        this.Model[val].src = require('@/assets/img/icon_collapse.png');
        this.Model[val].flag = false;
      } else {
        this.Model[val].src = require('@/assets/img/icon_unfold.png');
        this.Model[val].flag = true;
      }
    },
    onDialogEdit(flag, val) {
      this.Model.editDialogFlag = flag;
      if (flag === false) return;
      const obj = this.Model[val];
      this.Model.editDialogFormData = _.cloneDeep(this.Model.defaultEditDialogFormData);
      this.getAddress();
    },
    changeAddress(val) {
      if (!val) {
        this.getAddress();
      }
    },
    getAddress() {
      const idcard = 4310555;
      this.$ajax.post(this.apiTest + `/sale/saleOrder/getAddressByIdCard/${idcard}`, {})
        .then(response => {
          const data = response.data;
          if (data.status == 200) {
            this.Model.addressList = data.result;
          }
        }).catch(err => {
          console.log(err)
        })
    },
    updateBaseSaleOrder() {
      const data = this.Model.editDialogFormData;
      let params;
      if (data.shippingMethod === '1') {
        params = {
          shippingMethod: data.shippingMethod,
          shipper: data.shipper,
          originChannel: data.originChannel,
          salesman: data.salesman,
        }
      } else {
        params = data;
      }
      this.$ajax.post(this.apiTest + `/sale/saleOrder/updateBaseSaleOrder`, params)
        .then(response => {
          const data = response.data;
          if (data.status == 200) {
            this.onDialogEdit(false);
            this.$message({
              type: 'success',
              message: '已成功保存'
            });
            this.requestDetail();
          } else {
            this.$message.error(data.msg);
          }
        }).catch(err => {
          this.$message.error(err);
          this.onDialogEdit(false);
        });
    },
  }
}
export default Controller;
